package com.sskj.qiangjin.ui.activity;


import android.text.TextUtils;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.jakewharton.rxbinding2.widget.RxAdapterView;
import com.sskj.common.base.App;
import com.sskj.common.util.ImageUtil;
import com.sskj.common.util.TimeFormatUtil;
import com.sskj.lib.BaseHttpConfig;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.bean.UserBean;
import com.sskj.lib.bean.UserData;
import com.sskj.lib.model.room.UserViewModel;
import com.sskj.qiangjin.R;
import com.sskj.qiangjin.component.DaggerUserDataComponent;
import com.sskj.qiangjin.presenter.UserInfoActivityPresenter;
import com.sskj.qiangjin.ui.ArrowAdapter;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;

@Route(path = RConfig.APP_USER_INFO)//用户信息
public class UserInfoActivity extends BaseActivity<UserInfoActivityPresenter> {
    @Inject
    UserViewModel userViewModel;
    @BindView(R.id.spinner)
    Spinner spinner;
    @BindView(R.id.tv_money_type)
    TextView tvMoneyType;
    @BindView(R.id.iv_user_header)
    ImageView ivUserHeader;
    @BindView(R.id.tv_now_profit)
    TextView tvNowProfit;
    @BindView(R.id.tv_bet_total_money)
    TextView tvBetTotalMoney;
    @BindView(R.id.tv_tallest_profit)
    TextView tvTallestProfit;
    @BindView(R.id.tv_tallest_loss)
    TextView tvTallestLoss;
    @BindView(R.id.tv_profit_rank)
    TextView tvProfitRank;
    @BindView(R.id.tv_bet_times)
    TextView tvBetTimes;
    @BindView(R.id.tv_register_data)
    TextView tvRegisterData;
    @BindView(R.id.tv_nick_name)
    TextView tvNickName;
    private UserData userData;

    @Override
    protected int getLayoutResId() {
        return R.layout.app_activity_user_info;
    }

    @Override
    public UserInfoActivityPresenter getPresenter() {
        return new UserInfoActivityPresenter();
    }

    @Override
    protected void initView() {
        setTitle("用户信息");
        mPresenter.getUserInfo("pb");
        DaggerUserDataComponent.create().inject(this);
        userViewModel.getUsers().observe(this, users -> {
            if (users != null && users.size() > 0) {
                userData = users.get(0);
            } else {
                userData = null;
            }
        });

        List<String> payTypes = Arrays.asList(getResources().getStringArray(R.array.app_rank_types));
        ArrowAdapter spinnerTypeAdapter = new ArrowAdapter(this,
                R.layout.app_item_user_type, payTypes);
        spinnerTypeAdapter.setDropDownViewResource(R.layout.app_item_user_type_drop);
        spinner.setAdapter(spinnerTypeAdapter);
        spinner.setSelection(0);
        spinner.setDropDownVerticalOffset(10);
        RxAdapterView.itemSelections(spinner)
                .subscribe(pos -> {
                    switch (pos) {
                        case 0:
                            mPresenter.getUserInfo("usdt");//金币
                            break;
                        case 1:
                            mPresenter.getUserInfo("cny");//USDT
                            break;
                        case 2:
                            mPresenter.getUserInfo("pb");//QC
                            break;
                        default:
                            break;
                    }
                });
    }

    public void updateUI(UserBean userBean) {
        if (TextUtils.isEmpty(userBean.getUpic())) {
            ivUserHeader.setImageDrawable(App.INSTANCE.getResources().getDrawable(R.mipmap.app_icon_wolf));
        } else {
            ImageUtil.setCircleImage(BaseHttpConfig.BASE_URL + userBean.getUpic(), ivUserHeader);
        }
        tvNowProfit.setText(userBean.getList().get(0).getIncome());
        tvBetTotalMoney.setText(userBean.getList().get(0).getLeiji());
        tvTallestProfit.setText(userBean.getList().get(0).getZuigao());
        tvTallestLoss.setText(userBean.getList().get(0).getLoss());
        tvProfitRank.setText(userBean.getList().get(0).getRanking());
        tvBetTimes.setText(userBean.getList().get(0).getNum());
        tvNickName.setText(userBean.getRealname());
        if (!TextUtils.isEmpty(userBean.getReg_time())) {
            tvRegisterData.setText("注册于 " + TimeFormatUtil.SF_FORMAT_F.format(new Date(Long.valueOf(Long.valueOf(userBean.getReg_time()) * 1000))));
        }
    }

}
